package com.trickytriads.model.datastore
{
	import com.trickytriads.model.PlayerData;
	
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	/**
	 * DataStore for PlayerData objects.  Handles lazy loading.
	 */
	public class PlayerDataStore extends EventDispatcher
	{
		/**
		 * Dictionary object that contains PlayerData.  Keys are player_ids, values are
		 * PlayerData objects.
		 */
		private var _dict:Dictionary;
		
		// Constructor that initializes the singleton
		public function PlayerDataStore()
		{
		}

		/**
		 * Adds or updates a player object in the dictionary.
		 */
		public function addOrUpdatePlayer (pdata:PlayerData) : void
		{
			if (_dict == null) // Create the dictionary
				_dict = new Dictionary();
				
			// Adding/updating checking is unnecessary, I think.  Just insert.
			// if (_dict.hasOwnProperty(pdata.FacebookID.toString())	
			_dict[pdata.FacebookID.toString()] = pdata;
		}

		/**
		 * Loads a player from the dictionary.
		 * 
		 * This should ONLY be called by the I_PlayerService implementation!
		 * 
		 * Returns either the PlayerData object or null.
		 */
		public function getPlayerById (pid:int) : PlayerData
		{
			return (_dict[pid.toString()] as PlayerData);
		}

	}
}